
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>call apply</title>
</head>
<body>
<input type="text" id="idTxt" value="input text">

<script>
    /***********************第一种情况*****************************/
    var value = "global var";
    function mFunc()
    {
        this.value = "member var";
    }

    function gFunc()
    {
        console.log(this.value);
    }

    window.gFunc();         // show gFunc, global var
    gFunc.call(window);        // show gFunc, global var
    gFunc.call(new mFunc());      // show mFunc, member var
    gFunc.call(document.getElementById('idTxt')); // show element, input text
    /***********************第二种情况*****************************/
    var func = new function()
    {
        this.a = "func";
    }

    var func2 = function(x)
    {
        var a = "func2";
        console.log(this.a);
        console.log(x);
    }
    func2.call(func, "func2");      // show func and func2
    /***********************第三种情况*****************************/

  /*  function baseA()  // base Class A
    {
        this.memberA= "baseA member";
        this.showSelfA = function()
        {
            window.alert(this.memberA);
        }
    }

    function baseB()  // base Class B
    {
        this.member = "baseB member";
        this.showSelfB = function()
        {
            window.alert(this.member);
        }
    }

    function extendAB()  // Inherit Class from A and B
    {
        baseA.call(this); // call for A
        baseB.call(this); // call for B
    }

    window.onload = function()
    {
        var extend = new extendAB();
        extend.showSelfA();  // show A
        extend.showSelfB();  // show B
    }*/

    /***********************第四种情况*****************************/
    var Class = {
        create: function()    // create Function
        {
            return function()
            {
                this.initialize.apply(this, arguments);
            }
        }
    };

    var Person = Class.create();  // Create Class Person
    Person.prototype = {    // prototype initialize
        initialize: function(obj1, obj2)
        {
            this.obj1 = obj1;
            this.obj2 = obj2;
        },
        showSelf: function()
        {
            console.log("obj: " + this.obj1 + " and " + this.obj2);
        }
    }

    // instance Class
    var person = new Person("man", "women"); // two params
    person.showSelf();       // show person


    /***********************第五种情况*****************************/
     var User={
     count:1,
     getCount:function(){
     return this.count;
     }
     };
     var User2={
     count:2

     };
    var c=User.getCount.call(User2);
    console.log(c);
     console.log(User.getCount());
     var func=User.getCount;
     console.log(func());
</script>
</body>
</html>